import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/ui/address_book/search_address_book_page.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/viewmodel/address_book/address_book_list_view_model.dart';
import 'package:ls_work/widgets/customer_under_line_tab_indicator.dart';

import 'address_book_content_fragment.dart';

class AddressBookListPage extends BaseStatefulMvvmWidget {
  @override
  State<StatefulWidget> createState() => _AddressBookListState();
}

class _AddressBookListState
    extends BaseMvvmState<AddressBookListViewModel, AddressBookListPage> {


  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      appBar: AppBar(
        title: Text("通讯录"),
        actions: [
          IconButton(onPressed: (){
            Go().push(SearchAddressBookPage(list: viewModel.listItems,));
          }, icon: Icon(Icons.search))
        ],
      ),
      body: loadingContentWidget,
    );
  }

  @override
  AddressBookListViewModel createViewModel() {
    return AddressBookListViewModel(context);
  }

  @override
  Widget? buildLoadingContentView() {
    return DefaultTabController(
        length: viewModel.listItems.length,
        child: Column(
          children: [
            TabBar(
              tabs: viewModel.listItems
                  .map(
                    (e) => Tab(
                      text: (e.officeName ?? "").ifEmpty("其他"),
                    ),
                  )
                  .toList(),
              indicator: CustomUnderlineTabIndicator(
                borderSide: BorderSide(color: Colors.blue,width: 4),
                width: 25
              ),
              indicatorPadding: EdgeInsets.only(bottom: 8),
              unselectedLabelColor: Colors.grey[700],
              labelColor: Colors.black,
              unselectedLabelStyle: TextStyle(fontSize: 15,fontWeight: FontWeight.normal),
              labelStyle: TextStyle(fontSize: 18,fontWeight: FontWeight.bold),
              isScrollable: true,
            ),
            TabBarView(children: viewModel.listItems.map((e) => AddressBookContentFragment(list: e.staffPhoneBooks??[],)).toList()).setWeight(1)
          ],
        ));
  }

  @override
  void onRetryClick() {
    viewModel.requestRefresh(showAni: false);
  }


}
